<?php

namespace App\Models;

use App\Presenter\PostPresenter;
use Eloquent as Model;
use Illuminate\Database\Eloquent\Builder;
use Prettus\Repository\Traits\PresentableTrait;

/**
 * Class Post
 *
 * @package App\Models
 * @version January 10, 2017, 1:57 am UTC
 * @property int $id
 * @property int $category_id 类别
 * @property string $title 标题
 * @property string $slug 锚点
 * @property string $summary 概要
 * @property string $thumb 缩略图
 * @property string $content 内容
 * @property int $comment_count 评论次数
 * @property int $view_count 浏览次数
 * @property int $favorite_count 点赞次数
 * @property int $published 文章是否发布
 * @property string $published_at 文章发布时间
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Post whereId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Post whereCategoryId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Post whereTitle($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Post whereSlug($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Post whereSummary($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Post whereThumb($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Post whereContent($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Post whereCommentCount($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Post whereViewCount($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Post whereFavoriteCount($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Post wherePublished($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Post wherePublishedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Post whereCreatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Post whereUpdatedAt($value)
 * @mixin \Eloquent
 * @property-read \App\Models\Category $category
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Comment[] $comments
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Tag[] $tags
 */
class Post extends Model
{
//    use SoftDeletes;

    use \App\Presenter\PresentableTrait;
    protected $presenter  = PostPresenter::class;

//    use PresentableTrait;

    public $table = 'posts';

    const CREATED_AT = 'created_at';
    const UPDATED_AT = 'updated_at';

    public static $PUBLISHED = 1;
    public static $PUBLISHED_NO = 2;



    protected $dates = ['deleted_at', 'published_at', 'created_at', 'updated_at'];


    public $fillable = [
        'category_id',
        'title',
        'slug',
        'summary',
        'thumb',
        'content',
        'comment_count',
        'view_count',
        'favorite_count',
        'published',
        'published_at'
    ];

    public static $rules = [
        'title' => 'required|max:30|min:2',
        'slug' => 'string',
        'summary' => 'required|max:140|min:2',
        'thumb' => 'file',
        'content' => 'required|min:10',
        'comment_count' => 'integer',
        'view_count' => 'integer',
        'favorite_count' => 'integer',
        'published' => 'required|in:1,2',
        'published_at' => 'required|date_format:Y-m-d H:m:s'
    ];

    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        'id' => 'integer',
        'category_id' => 'integer',
        'title' => 'string',
        'slug' => 'string',
        'summary' => 'string',
        'thumb' => 'string',
        'content' => 'string',
        'comment_count' => 'integer',
        'view_count' => 'integer',
        'favorite_count' => 'integer'
    ];

//    public function setPublishedAttribute($published){
//        $this->published=self::PUBLISHED;
//    }
    public function category()
    {
        return $this->belongsTo(Category::class);
    }

    public function comments()
    {
        return $this->hasMany(Comment::class);
    }

    public function tags()
    {
        return $this->belongsToMany(Tag::class);
    }

    /**
     * @param $query Builder
     * @return mixed
     */
    public function scopePublished($query)
    {
        return $query->where('published', self::PUBLISHED);
    }

//    public function scopePublished($query)
//    {
//        $query->where('published_at','<=',Carbon::now());
//    }
    protected function setPublishedAttribute()
    {
        $this->published = self::$PUBLISHED_NO;
    }

//    protected function getPublishedAttribute($value)
//    {
//        if ($value == self::$PUBLISHED_NO) {
//            return \Lang::get('label.post.unPublished');
//        } elseif ($value == self::$PUBLISHED) {
//            return \Lang::get('label.post.published');
//        }
//        return \Lang::get('label.post.published');
//    }

    public static function getPublished()
    {
        return self::PUBLISHED;
    }

    public static function getPublishedNo()
    {
        return self::PUBLISHED_NO;
    }

//    protected function getPrevArticleId($query)
//    {
//        return $thiswhere('id', '<', $id)->max('id');
//    }

//    public function setSummaryAttribute($value){
//
//    }
//
//    public function getCreatedAtAttribute($value){
//
//    }




    public function getAmountAttribute($value)
    {
        return $value / 100;
    }

    public function setAmountAttribute($value)
    {
        $this->attributes['amount'] = (int)($value * 100);
    }


}
